/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#include "pch.h"
#include <journal/models/ink_surface_model_factory.h>
#include <journal/models/db/ink_surface_table.h>
#include <journal/models/ink_surface_model.h>

using namespace journal::model;

b::uint32 ink_surface_model_factory::_typeToken = 0;

ink_surface_model_factory::ink_surface_model_factory(std::shared_ptr<s::iengine> dbEngine) :
    _nextId(0),
    _dbEngine(dbEngine)
{
    if (_typeToken == 0)
    {
        _typeToken = constants::undoRedoService()->generateTargetTypeToken();

        constants::memoryTrackingService()->registerType(_typeToken, L"ink surface model", true);

        DEBUG_CONSOLE_TRACE_TAG(TRACE_TAG_GENERIC_MODEL,
            boost::wformat(L"ink_surface_model_factory::ink_surface_model_factory() : type token = %i") %
            _typeToken
            );
    }
}

std::shared_ptr<iink_surface_model_factory> ink_surface_model_factory::create(std::shared_ptr<s::iengine> dbEngine)
{
    static bool instantiated = false;
    static std::shared_ptr<ink_surface_model_factory> returnValue = std::make_shared<ink_surface_model_factory>(dbEngine);
    static std::recursive_mutex mutex;

    BAJA_LOCK(mutex);
    if (instantiated == false)
    {
        returnValue->_inkSurfaceTable = journal::model::db::createInkSurfaceTable(dbEngine);
        returnValue->_inkSurfaceTable->common()->initialize();
        returnValue->_nextId = returnValue->_inkSurfaceTable->common()->getNextId();
        instantiated = true;
    }
    return returnValue;
}

void ink_surface_model_factory::getPersistedInkSurfaceIds(b::uint32 pageId, std::vector<b::uint32>& ids)
{
    _inkSurfaceTable->readIds(pageId, ids);
}

void ink_surface_model_factory::removeFromDB(const b::uint32& pageId)
{
    journal::model::removeInkSurfaces(_inkSurfaceTable, pageId);
}

std::shared_ptr<iink_surface_model> ink_surface_model_factory::createInkSurface(
    const std::shared_ptr<ipage_model>& parent)
{
    return journal::model::createInkSurface(_typeToken, parent, _inkSurfaceTable, _nextId++);
}

std::shared_ptr<iink_surface_model> ink_surface_model_factory::recreateInkSurface(
    const std::shared_ptr<ipage_model>& parent,
    const b::uint32 inkSurfaceId)
{
    return journal::model::createInkSurface(_typeToken, parent, _inkSurfaceTable, inkSurfaceId);
}

std::shared_ptr<iink_surface_model_factory> journal::model::createInkSurfaceModelFactory(std::shared_ptr<s::iengine> dbEngine)
{
    return ink_surface_model_factory::create(dbEngine);
}
